<?php
namespace app\blog\loglic;

use app\common\loglic\Model;

class Type extends Model
{
    //初始查询参数
    private $args = [
        'where' => [
            ['type_module','eq','blog'],
        ],
        'cache' => false,
        'order' => [
            'type_id' => 'desc',
        ],
    ];

    //修改器（写入数据库前）
    private function dataSet($data=[])
    {
        $data['type_slug']   = $this->autoSlug($data['type_slug'],$data['type_name']);
        $data['type_module'] = 'blog';
        return ['type'=>$data];
    }

    //获取器（查询数据库后）
    private function dataGet($data=[])
    {
        return $data;
    }

    /**
     * 添加
     * @version 1.0.0 首次引入
     * @param array $data 必需;数组格式;默认:空
     * @return int 返回自增ID或0
     */
    public function dataSave($data=[])
    {
        //数据初始化
        unset($data['type_id']);
        //数据修改器
        $data = $this->dataSet($data);
        //表单验证规则
        $this->validateRule = [
            'type_name' => 'unique:type,type_name^type_module',
            'type_slug' => 'unique:type,type_slug^type_module',
        ];
        $this->validateMsg = [
            'type_name.unique' => '分类名称已存在',
            'type_slug.unique' => '唯一标识已存在',
        ];
        if(!$this->validateData($data['type'])){
            return false;
        }
        //写入数据库
        return $this->modelSave('common/Type',$data);
    }

    //修改
    public function dataUpdate($data=[])
    {
        //数据修改器
        $data = $this->dataSet($data);
        //表单验证规则
        $this->validateRule = [
            'type_id' => 'number',
            'type_name' => 'unique:type,type_name^type_module',
            'type_slug' => 'unique:type,type_slug^type_module',
        ];
        $this->validateMsg = [
            'type_id.number' => 'ID错误',
            'type_name.unique' => '分类名称已存在',
            'type_slug.unique' => '唯一标识已存在',
        ];
        //表单验证未通过
        if(!$this->validateData($data['type'])){
            return false;
        }
        //更新条件
        $where = [
            'type_id' => $data['type']['type_id'],
        ];
        return $this->modelUpdate('common/Type', $where, $data);
    }

    //按条件分类列表
    public function all($args=[])
    {
        //合并参数
        $args = array_merge($this->args,$args);
        //初始参数
        array_push($args['where'],['type_module','eq','blog']);
        //查询数据
        return $this->modelSelect('common/Type', $args);
    }

    //按条件获取分类列表树状
    public function allTree($args=[])
    {
        $result = $this->all($args);
        if($result){
            $result = model('common/Tree','loglic')->list_to_tree($result,'type_id','type_parent');
        }
        return $result;
    }

    //按条件获取层级列表
    public function allLevel($args=[])
    {
        return model('common/Tree','loglic')->tree_to_level($this->allTree($args),'type_name');
    }

    //权重值增减
    public function weightUpdate($typeId=0, $value=0)
    {
        if(!$typeId || !$value){
            return false;
        }
        if($value>0){
            return model('common/Type','loglic')->incUpdate($typeId, 'type_weight', $value);
        }else{
            return model('common/Type','loglic')->decUpdate($typeId, 'type_weight', $value);
        }
    }

    //获取字段最大值
    public function getWeightMax()
    {
        return $this->dbMax('type','type_weight')+1;
    }

    //按ID查询单个数据
    public function getId($id=0, $cache=false, $status=false)
    {
        $where = [
            ['type_id','eq',$id],
        ];
        if($status){
            array_push($where,['type_status','eq',$status]);
        }
        $result = $this->modelGet('common/Type',[
            'cache' => $cache,
            'where' => $where,
        ]);
        return $this->dataGet($result);
    }

    //按ID查询单个数据
    public function getSlug($slug=0, $cache=false, $status=false)
    {
        $where = [
            ['type_slug','eq',$slug],
            ['type_module','eq','blog'],
        ];
        if($status){
            array_push($where,['type_status','eq',$status]);
        }
        $result = $this->modelGet('common/Type',[
            'cache' => $cache,
            'where' => $where,
            'with'  => $with,
        ]);
        return $this->dataGet($result);
    }

    //返回所有分类
    public function listAll($field='type_id,type_name,type_parent')
    {
        return db('type')->field($field)->limit(0)->where([
            ['type_module','eq','blog'],
        ])->order([
            'type_id' => 'desc',
        ])->select();
    }

    //返回N条列结果键名为ID键值为Name
    public function listOption($limt=10)
    {
        return db('type')->limit($limt)->where([
            ['type_status','eq','normal'],
            ['type_module','eq','blog'],
        ])->order([
            'type_parent' => 'asc',
            'type_weight' => 'desc',
            'type_id' => 'desc',
        ])->column('type_name','type_id');
    }

    //所回树状分类列表选项
    public function listOptionLevel($limt=0)
    {
        $result = db('type')->field('type_id,type_name,type_parent')->limit($limt)->where([
            ['type_status','eq','normal'],
            ['type_module','eq','blog'],
        ])->order([
            'type_weight' => 'desc',
            'type_id' => 'desc',
        ])->select();
        if($result){
            $result = model('common/Tree','loglic')->list_to_tree($result,'type_id','type_parent');
            $result = model('common/Tree','loglic')->tree_to_level($result,'type_name');
        }
        $result = array_column($result,'type_name','type_id');
        $result[0] = '------';
        return $result;
    }
}